

-- Namespace
	local _,mod = ...


-- Object ref
	local RAID_CLASS_COLORS = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS


-- Constants
	local font = {mod.path..'media\\bavaria.ttf',8,'OUTLINEMONOCHROME'}
	
	local c_bar =    {.16,.16,.16}
	local c_barbg =  {.33,.33,.33}
	local c_border = {.07,.07,.07}
	
	local white = { r = 1, g = 1, b = 1 }


-- Helper
	local function abbrev(text)
		text = text:sub(1,1)
		text = text..'.'
		return text
	end


-- Nameplate construction
	function mod.newplate()
		local plate = mod:CreateFrame('Frame',nil,UIParent)
			plate:SetFrameLevel(1)
			plate:SetHeight(10) plate:SetWidth(140)
			plate:Listen('reaction',function(self,type)
				if type == 'docile' then
					self:Hide()
				end
			end)
		
		local hpbar = plate:CreateTexture(nil,'ARTWORK')
			hpbar:SetHeight(3) hpbar:SetWidth(138)
			hpbar:SetPoint('BOTTOMLEFT',1,1)
			hpbar:SetTexture(unpack(c_bar))
			hpbar:Listen('health',function(self,min,max)
				local perc = min / max
				if perc <= 0 then
					perc = .0001
				elseif perc > 1 then
					perc = 1
				end
				self:SetWidth(138*perc)
			end)
			plate.hpbar = hpbar
		
		local hpbg = plate:CreateTexture(nil,'BORDER')
			hpbg:SetHeight(3)
			hpbg:SetPoint('BOTTOMRIGHT',-1,1)
			hpbg:SetPoint('LEFT',hpbar,'RIGHT')
			hpbg:SetTexture(unpack(c_barbg))
			plate.hpbg = hpbg
		
		local hpborder = plate:CreateTexture(nil,'BACKGROUND')
			hpborder:SetHeight(5) hpborder:SetWidth(140)
			hpborder:SetPoint('BOTTOMLEFT')
			hpborder:SetTexture(unpack(c_border))
			plate.hpborder = hpborder
		
		local hptext = plate:CreateFontString(nil,'OVERLAY')
			hptext:SetPoint('BOTTOMRIGHT',2,7)
			hptext:SetFont(unpack(font))
			hptext:SetJustifyH('RIGHT')
			hptext:Listen('health',function(self,min)
				local text
				if min > 1000000 then
					text = ('%.1fM'):format(min/1000000)
				elseif min > 9999 then
					text = ('%.1fk'):format(min/1000)
				else
					text = ('%d'):format(min)
				end
				self:SetText(text)
			end)
			plate.hptext = hptext
		
		
		local nametext = plate:CreateFontString(nil,'OVERLAY')
			nametext:SetPoint('BOTTOMLEFT',0,7)
			nametext:SetFont(unpack(font))
			nametext:SetJustifyH('LEFT')
			nametext:Listen('name',function(self,name)
				name = name:gsub('(%S+) ',abbrev)
				self:SetText(name)
			end)
			nametext:Listen('reaction',function(self,type)
				if type ~= 'enemyplayer' then
					self:SetTextColor(.6,.6,.6)
				end
			end)
			nametext:Listen('class',function(self,class)
				local color = RAID_CLASS_COLORS[class] or white
				self:SetTextColor(color.r,color.g,color.b)
			end)
			plate.nametext = nametext
		
		return plate
	end
